package com.huan.pdf.controller;

import com.huan.pdf.utils.FreemarkerUtils;
import com.huan.pdf.utils.PdfUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * pdf控制器
 *
 * @author admin
 */
@RestController
public class PdfController {

    @GetMapping("pdf")
    public void pdf(HttpServletResponse response) throws IOException {
        Map<String, Object> params = new HashMap<>(16);
        params.put("mainTitle", "这是第一个Pdf-A4-竖向 - " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        // 渲染模板
        String firstHtmlContent = FreemarkerUtils.processTemplate("pdf-first.ftl", params);

        params.put("mainTitle", "这是第二个Pdf-A3-横向 - " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        // 渲染模板
        String secondHtmlContent = FreemarkerUtils.processTemplate("pdf-second.ftl", params);

        // pdf合并
        PdfUtils.mergePdf(response, firstHtmlContent, secondHtmlContent);
    }
}
